package com.javabase.study.OOPFistPart;

/**
 * TODO
 * 内容：equals方法测试和自定义重写equals方法
 * 作者:jxx
 * 创建时间：2021/6/15
 */
public class TestEquals {
    public static void main(String[] args){

        Person1 p1 = new Person1(111,"jiangxx");
        Person1 p2 = new Person1(111,"gaoyang");

        System.out.println(p1 == p2);//false,不是同一个对象
        System.out.println(p1.equals(p2));//true，id相同则认为两个对象内容相同

        String s1 = new String("jiangxx");
        String s2 = new String("jiangxx");
        System.out.println(s1 == s2); //false 两个对象并不不是同一个对象
        System.out.println(s1.equals(s2)); //true 两个字符串内容相同
    }
}
class Person1{

    int id;
    String name;
    public Person1(int id,String name){

        this.id = id;
        this.name = name;
    }
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }else {
            if(obj instanceof Person1){
                Person1 c = (Person1)obj;
                if (c.id == this.id){
                    return true;
                }
            }
        }
        return false;
    }

}


